<?php

namespace util\kuaidi\driver;
class KuaidiNiao{
    
    
    private $EBusinessID;   //商户ID
    private $AppKey;        //秘钥
    private $ReqURL;        //接口地址
    
    public function __construct(){
       // $this->EBusinessID  = 1338626;                               //基利斯
       // $this->AppKey       = '34935e24-f156-4853-beb9-d85741b10ba3';//基利斯
        $this->EBusinessID  = 1567870;                               // 
        $this->AppKey       = '833f96b2-8b27-4403-a702-50ca232fbbd1';//
        // $this->EBusinessID  = 1294391;                                  //奇龙网
        // $this->AppKey       = '835b39f3-73aa-4422-b3d6-d2d5ebd4a0ce';   //奇龙网
        $this->ReqURL       = 'http://api.kdniao.cc/Ebusiness/EbusinessOrderHandle.aspx';
        
    } 
    
    
    /**
     * 物流跟踪
     * @param $no 快递单号
     * @param $ShipperCode 快递公司的代号
     * 物流状态:0-无轨迹2-在途中3-签收4-问题件
     */
    public function query($no,$ShipperCode = ''){
        /*
        $ShipperCode = config('kuaidi.KD_KDNIAO_CODE')[$ShipperCode];
        if(!$ShipperCode){
            $noInfo =  $this->identification($no);
            $ShipperCode = $noInfo['Shippers'][0]['ShipperCode'];
        }
        */
        $requestData= "{'OrderCode':'','ShipperCode':'".$ShipperCode."','LogisticCode':".$no."}";
        $requestData= "{'OrderCode':'','ShipperCode':'YTO','LogisticCode':'12345678'}";
    	$data = array(
            'RequestType' => '1002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $data['DataSign'] = $this->encrypt($requestData);
        $data['EBusinessID']    = $this->EBusinessID;
        $info = $this->sendPost($this->ReqURL,$data);
    	return array(
            'success'   => $result['Success'],
            'state'     => $result['State'],
            'traces'    => array_reverse($result['Traces'])
        );
    }
    
    
    /**
     * 订单号识别
     */
    public function identification($no){
        $requestData= "{'LogisticCode':".$no."}";
    	$data = array(
            'RequestType' => '2002',
            'RequestData' => urlencode($requestData) ,
            'DataType' => '2',
        );
        $data['DataSign'] = $this->encrypt($requestData);        
        $data['EBusinessID']    = $this->EBusinessID;
        dump($this->ReqURL);
        dump($data);
        $info = $this->sendPost($this->ReqURL,$data);
    	return $result;
    }
    
    /**
     * Sign签名生成
     * @param data 内容   
     * @param appkey Appkey
     * @return DataSign签名
     */
    protected  function encrypt($data) {        
        return urlencode(base64_encode(md5($data.$this->AppKey)));
    }
    
    
    
    /**
     *  post提交数据 
     * @param  string $url 请求Url
     * @param  array $data 提交的数据 
     * @return url响应返回的html
     */
     
    function sendPost($url, $datas) {
        $temps = array();	
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);		
        }	
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
    	if(empty($url_info['port']))
    	{
    		$url_info['port']=80;	
    	}
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
    	$headerFlag = true;
    	while (!feof($fd)) {
    		if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
    			break;
    		}
    	}
        while (!feof($fd)) {
    		$gets.= fread($fd, 128);
        }
        fclose($fd);  
        
        return $gets;
    }
    private function sendPost_b($url, $data){
        dump($url);
        dump($data);
        $result = \org\net\Http::curl($url,$data,'POST');
        $temps = array();	
        foreach ($data as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);		
        }	
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
    	if(empty($url_info['port']))
    	{
    		$url_info['port']=80;	
    	}
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        dump( function_exists('fsockopen'));
        
        exit;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
    	$headerFlag = true;
    	while (!feof($fd)) {
    		if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
    			break;
    		}
    	}
        while (!feof($fd)) {
    		$gets.= fread($fd, 128);
        }
        fclose($fd);
        return $gets;
    }
    
    
}